/*
 * defines.h
 *
 *  Created on: Jan 14, 2014
 *      Author: ppks
 */

#ifndef DEFINES_H_
#define DEFINES_H_

#include "config.h"

/********************************************************************************/
/* MCU specific definitions                                                     */
/********************************************************************************/
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
  #define MEGA
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
  #define PROMINI
#elif defined(__AVR_ATmega32U4__) || defined(TEENSY20)
  #define PROMICRO
#endif

#endif /* DEFINES_H_ */

/********************************************************************************/
/* Board specific pinout configuration                                          */
/********************************************************************************/
#if defined(MEGA)

    #define I2C_PULLUPS_ENABLE         PORTD |= 1<<0; PORTD |= 1<<1;       /* PIN 20&21 (SDA&SCL) */
    #define I2C_PULLUPS_DISABLE        PORTD &= ~(1<<0); PORTD &= ~(1<<1);

#endif

#if (REMOTE_CONTROL_RECEIVER_TYPE == RC_TYPE_STANDARD_RX)
    #define RC_CHANNELS_NUM             8
#endif /* REMOTE_CONTROL_RECEIVER_TYPE == RC_TYPE_STANDARD_RX */

/**************************  all the Mega types  ***********************************/
#if defined(MEGA)
    #define LEDPIN_SET_PINMODE          pinMode (13, OUTPUT);pinMode (30, OUTPUT);
    #define LEDPIN_TOGGLE               PINB  |= (1<<7); PINC  |= (1<<7);
    #define LEDPIN_ON                   PORTB |= (1<<7); PORTC |= (1<<7);
    #define LEDPIN_OFF                  PORTB &= ~(1<<7);PORTC &= ~(1<<7);
    #define BUZZERPIN_SET_PINMODE       pinMode (32, OUTPUT);
  #if defined(PILOTLAMP)
    #define PL_PIN_ON                   PORTC |= 1<<5;
    #define PL_PIN_OFF                  PORTC &= ~(1<<5);
    /* Note: If pilot lamp is defined - buzzer pin will be defined using pilot lamp command later */
  #else /* defined(PILOTLAMP) */
    #define BUZZERPIN_ON                PORTC |= 1<<5;
    #define BUZZERPIN_OFF               PORTC &= ~(1<<5);
  #endif /* defined(PILOTLAMP) */

  #if !defined(DISABLE_POWER_PIN)
    #define POWERPIN_SET_PINMODE        pinMode (37, OUTPUT);
    #define POWERPIN_ON                 PORTC |= 1<<0;
    #define POWERPIN_OFF                PORTC &= ~(1<<0);
  #else
    #define POWERPIN_SET_PINMODE        ;
    #define POWERPIN_ON                 ;
    #define POWERPIN_OFF                ;
  #endif
    #define I2C_PULLUPS_ENABLE          PORTD |= 1<<0; PORTD |= 1<<1;       // PIN 20&21 (SDA&SCL)
    #define I2C_PULLUPS_DISABLE         PORTD &= ~(1<<0); PORTD &= ~(1<<1);
    #define LCD_SET_PINMODE             pinMode(0, OUTPUT);
    #define LCDPIN_OFF                  PORTE &= ~1; //switch OFF digital PIN 0
    #define LCDPIN_ON                   PORTE |= 1;
    #define STABLEPIN_SET_PINMODE       pinMode (31, OUTPUT);
    #define STABLEPIN_ON                PORTC |= 1<<6;
    #define STABLEPIN_OFF               PORTC &= ~(1<<6);
  #if defined(PPM_ON_THROTTLE)
    /* configure THROTTLE PIN (A8 pin) as input witch pullup and enabled PCINT interrupt */
    #define PPM_PIN_INTERRUPT           DDRK &= ~(1<<0); PORTK |= (1<<0);  PCICR |= (1<<2); PCMSK2 |= (1<<0);
  #else /* defined(PPM_ON_THROTTLE) */
    #define PPM_PIN_INTERRUPT           attachInterrupt(4, rxInt, RISING);  /* PIN 19, also used for Spektrum satellite option */
  #endif /* defined(PPM_ON_THROTTLE) */
  #if !defined(SPEK_SERIAL_PORT)
    #define SPEK_SERIAL_PORT            1
  #endif /* !defined(SPEK_SERIAL_PORT) */
    /* RX PIN assignment inside the port for PORTK */
    #define THROTTLEPIN                 0    /* PIN 62 =  PIN A8 */
    #define ROLLPIN                     1    /* PIN 63 =  PIN A9 */
    #define PITCHPIN                    2    /* PIN 64 =  PIN A10 */
    #define YAWPIN                      3    /* PIN 65 =  PIN A11 */
    #define AUX1PIN                     4    /* PIN 66 =  PIN A12 */
    #define AUX2PIN                     5    /* PIN 67 =  PIN A13 */
    #define AUX3PIN                     6    /* PIN 68 =  PIN A14 */
    #define AUX4PIN                     7    /* PIN 69 =  PIN A15 */
    #define V_BATPIN                    A0   /* Analog PIN 0 */
    #define PSENSORPIN                  A2   /* Analog PIN 2 */
    #define PCINT_PIN_COUNT             8
    #define PCINT_RX_BITS               (1<<2),(1<<4),(1<<5),(1<<6),(1<<7),(1<<0),(1<<1),(1<<3)
    #define PCINT_RX_PORT               PORTK
    #define PCINT_RX_MASK               PCMSK2
    #define PCIR_PORT_BIT               (1<<2)
    #define RX_PC_INTERRUPT             PCINT2_vect
    #define RX_PCINT_PIN_PORT           PINK
#endif /* defined(MEGA) */

#if defined PILOTLAMP
    #define PL_CHANNEL                  OCR0B       /* use B since A can be used by camstab */
    #define PL_ISR                      TIMER0_COMPB_vect
    #define PL_INIT                     {TCCR0A = 0; TIMSK0 |= (1 << OCIE0B); PL_CHANNEL = PL_IDLE; PilotLamp(PL_GRN_OFF); PilotLamp(PL_BLU_OFF); PilotLamp(PL_RED_OFF); PilotLamp(PL_BZR_OFF);}
    #define BUZZERPIN_ON                PilotLamp(PL_BZR_ON);
    #define BUZZERPIN_OFF               PilotLamp(PL_BZR_OFF);
    #define PL_GRN_ON                   25        /* 100us */
    #define PL_GRN_OFF                  50        /* 200us */
    #define PL_BLU_ON                   75        /* 300us */
    #define PL_BLU_OFF                  100       /* 400us */
    #define PL_RED_ON                   125       /* 500us */
    #define PL_RED_OFF                  150       /* 600us */
    #define PL_BZR_ON                   175       /* 700us */
    #define PL_BZR_OFF                  200       /* 800us */
    #define PL_IDLE                     125       /* 100us */
#endif /* defined PILOTLAMP */

#if defined(PILOTLAMP)
    /* Force define BUZZER if pilot lamp is defined */
    #ifndef BUZZER
        #define BUZZER
    #endif /* #ifndef BUZZER */
#endif /* defined(PILOTLAMP) */

/******************************************************************************/
/* Sensor Type Definition                                                     */
/******************************************************************************/
/* Accelerometer */
#if (ACCELEROMETER_TYPE == MPU6050)
    #define ACC_INSTALLED       1
#else
    #define ACC_INSTALLED       0
#endif

/* Gyroscope */
#if (GYROSCOPE_TYPE == MPU6050)
    #define GYRO_INSTALLED      1
#else
    #define GYRO_INSTALLED      0
#endif

/* Magnetometer */
#if (MAGNETOMETER_TYPE == HMC5883)
    #define MAG_INSTALLED       1
#else
    #define MAG_INSTALLED       0
#endif

/* Barometer */
#if (BAROMETER_TYPE == MS561101BA)
    #define BARO_INSTALLED      1
#else
    #define BARO_INSTALLED      0
#endif

#if defined(GPS_SERIAL_ENABLED)
	#define GPS_INSTALLED       1
    #define NAV_CAP             1
#else /* defined(GPS_SERIAL_ENABLED) */
	#define GPS_INSTALLED       0
    #define NAV_CAP             0
#endif /* defined(GPS_SERIAL_ENABLED) */

#define SONAR_INSTALLED         0

#if defined(EXTENDED_AUX_STATES)
    #define EXTAUX_CAP          1
    #define AUX_BOX_ITEM_SIZE   4
#else /* defined(EXTENDED_AUX_STATES) */
    #define EXTAUX_CAP          0
    #define AUX_BOX_ITEM_SIZE   2
#endif /* defined(EXTENDED_AUX_STATES) */

/******************************************************************************/
/* Motor and Servo Configuration                                              */
/******************************************************************************/
#if defined(MULTICOPTER_TYPE_QUAD_X) || defined(MULTICOPTER_TYPE_QUAD_P)
    #define NUMBER_OF_MOTORS    4
#endif

/******************************************************************************/
/* Multitype decleration for the GUI's                                        */
/******************************************************************************/
#if defined(TRI)
  #define MULTITYPE 1
#elif defined(MULTICOPTER_TYPE_QUAD_P)
  #define MULTITYPE 2
#elif defined(MULTICOPTER_TYPE_QUAD_X)
  #define MULTITYPE 3
#elif defined(BI)
  #define MULTITYPE 4
  #define SERVO_RATES      {30,30,100,100,0,1,100,100}
#elif defined(GIMBAL)
  #define MULTITYPE 5
#elif defined(Y6)
  #define MULTITYPE 6
#elif defined(HEX6)
  #define MULTITYPE 7
#elif defined(FLYING_WING)
  #define MULTITYPE 8
  #define SERVO_RATES      {30,30,100,0,1,100,100,100}
#elif defined(Y4)
  #define MULTITYPE 9
#elif defined(HEX6X)
  #define MULTITYPE 10
#elif defined(OCTOX8)
  #define MULTITYPE 11   //the JAVA GUI is the same for all 8 motor configs
#elif defined(OCTOFLATP)
  #define MULTITYPE 12   //12  for MultiWinGui
#elif defined(OCTOFLATX)
  #define MULTITYPE 13   //13  for MultiWinGui
#elif defined(AIRPLANE)
  #define MULTITYPE 14
  #define SERVO_RATES      {30,30,100,100,-100,100,100,100}
#elif defined (HELI_120_CCPM)
  #define MULTITYPE 15
#elif defined (HELI_90_DEG)
  #define MULTITYPE 16
  #define SERVO_RATES      {30,30,100,-100,-100,100,100,100}
#elif defined(VTAIL4)
  #define MULTITYPE 17
#elif defined(HEX6H)
  #define MULTITYPE 18
#elif defined(SINGLECOPTER)
  #define MULTITYPE 20
  #define SERVO_RATES      {30,30,100,0,1,0,1,100}
#elif defined(DUALCOPTER)
  #define MULTITYPE 20
#endif
